<?php

class models_PrinttimesheetMapper extends models_MapperBase
{
    public static $_dbTable = 'models_DbTable_Printtimesheet';

//----------------------------------------------------------------------------------------------------

    protected static function _initItem($oRow)
    {
        if (null === $oRow)
        {
            return null;
        }

        $oItem = new models_Usertoproject();

        if (isset($oRow->id))
            $oItem->id              = $oRow->id;
        if (isset($oRow->id_user))
            $oItem->id_user         = $oRow->id_user;
        if (isset($oRow->id_project))
            $oItem->date            = $oRow->date;
        if (isset($oRow->file_name))
            $oItem->file_name       = $oRow->file_name;
                 

        return $oItem;
    }

//----------------------------------------------------------------------------------------------------

    public static function getAll($page = 1, $countItemsOnPage = 10)
    {
        $db = self::_getDbTable(self::$_dbTable);
        $select  = $db->select();

        $select->from($db, array('*'));
        $select->setIntegrityCheck(false);

//      $select->order('id DESC');
//      echo $select; die;

        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
        $paginator->setItemCountPerPage($countItemsOnPage)
                  ->setCurrentPageNumber($page);

        $result = array();
        foreach ($paginator as $page)
        {
            $result[] = (object)$page;
        }

        $ret['array'] = $result;
        $ret['paginator'] = $paginator;

        return $ret;
    }

//----------------------------------------------------------------------------------------------------

    public static function findByIdUserAndDate($id, $date)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
        $select = $DbTable->select();
        $select->setIntegrityCheck(false);

        $select->from($DbTable, array('*'));
        $select->where('id_user = ?', $id);
        $select->where('date = ?  ' , $date);

//      echo $select;die;
        $result = $DbTable->fetchAll($select);

        return $result->toArray();
    }

//----------------------------------------------------------------------------------------------------
    
    public static function findRecord($idUser, $idProject, $date)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
        $select = $DbTable->select();
        $select->setIntegrityCheck(false);

        $select->from($DbTable, array('*'));
        $select->where('id_user = ?', $idUser);
        $select->where('id_project = ?', $idProject);
        $select->where('date = ?', $date);

//      echo $select;die;
        $result = $DbTable->fetchRow($select);

        return self::_initItem($result);
    }
    
    
//----------------------------------------------------------------------------------------------------

    public static function findByIdProject($id)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
        $select = $DbTable->select();
        $select->setIntegrityCheck(false);

        $select->from($DbTable, array('*'));
        $select->where('id_project = ?', $id);

//      echo $select;die;
        $result = $DbTable->fetchRow($select);

        return self::_initItem($result);
    }

//----------------------------------------------------------------------------------------------------

    public static function save($data)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
        $result = $DbTable->insert($data);
        return $result;
    }

//----------------------------------------------------------------------------------------------------

    public static function update($id, $data)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
        $Update = $DbTable->update($data,  'id = ' . $id);

        return $Update;
    }

//----------------------------------------------------------------------------------------------------

    public static function delete($id)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
        $Delete  = $DbTable->delete('id = ' . $id);

        return $Delete;     
    }   
    
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------

}
